import java.text.*;
import java.util.*;
import java.io.*;

public abstract class FileTasks
{
    public FileTasks()
    {
    }
    private static String getValue(String string)
    {
        String[] value = string.split("=");
        if(value.length > 1)
            return value[1];
        else
            return "";
    }
    private static ID.PD getPersonalDataID(String string)
    {
        String[] aux = string.split("=");
        if(aux[0].equals("name"))
            return ID.PD.NAME;
        else if(aux[0].equals("lastName"))
            return ID.PD.LAST_NAME;
        else if(aux[0].equals("gender"))
            return ID.PD.GENDER;
        else if(aux[0].equals("occupation"))
            return ID.PD.OCCUPATION;
        else if(aux[0].equals("age"))
            return ID.PD.AGE;
        else if(aux[0].equals("coachsName"))
            return ID.PD.COACHS_NAME;
        else if(aux[0].equals("sport"))
            return ID.PD.SPORT;
        else if(aux[0].equals("category"))
            return ID.PD.CATEGORY;
        else if(aux[0].equals("institution"))
            return ID.PD.INSTITUTION;
        else if(aux[0].equals("dateBirth"))
            return ID.PD.DATE_BIRTH;
        return ID.PD.NONE;
    }
    private static ID.OTHER getOtherID(String string)
    {
    	String[] aux = string.split("=");
    	if(aux[0].equals("date"))
            return ID.OTHER.DATE;
    	else if(aux[0].equals("diagnostic"))
    		return ID.OTHER.DIAGNOSTIC;
		return ID.OTHER.NONE;
    }
    private static ID.A getAnthropometryID(String string)
    {
        String[] aux = string.split("=");
        if(aux[0].equals("currentWeight"))
            return ID.A.CURRRENT_WEIGHT;
        else if(aux[0].equals("targetWeight"))
            return ID.A.TARGET_WEIGHT;
        else if(aux[0].equals("size"))
            return ID.A.SIZE;
        return ID.A.NONE;
    }
    private static ID.A_CREASE getAnthropometryCreaseID(String string)
    {
        String[] aux = string.split("=");
        if(aux[0].equals("c_subscapularis"))
            return ID.A_CREASE.SUBSCAPULARIS;
        else if(aux[0].equals("c_tricep"))
            return ID.A_CREASE.TRICEP;
        else if(aux[0].equals("c_bicep"))
            return ID.A_CREASE.BICEP;
        else if(aux[0].equals("c_forearm"))
            return ID.A_CREASE.FOREARM;
        else if(aux[0].equals("c_supraspinal"))
            return ID.A_CREASE.SUPRASPINAL;
        else if(aux[0].equals("c_suprailiac"))
            return ID.A_CREASE.SUPRAILIAC;
        else if(aux[0].equals("c_abdLeft"))
            return ID.A_CREASE.ABD_LEFT;
        else if(aux[0].equals("c_abdRight"))
            return ID.A_CREASE.ABD_RIGHT;
        else if(aux[0].equals("c_thigh"))
            return ID.A_CREASE.THIGH;
        else if(aux[0].equals("c_leg"))
            return ID.A_CREASE.LEG;
        else if(aux[0].equals("c_pectoral"))
            return ID.A_CREASE.PECTORAL;
        return ID.A_CREASE.NONE;
    }
    private static ID.A_GIRTH getAnthropometryGirthID(String string)
    {
        String[] aux = string.split("=");
        if(aux[0].equals("g_relaxedArm"))
            return ID.A_GIRTH.RELAXED_ARM;
        else if(aux[0].equals("g_contractedArm"))
            return ID.A_GIRTH.CONTRACTED_ARM;
        else if(aux[0].equals("g_forearm"))
            return ID.A_GIRTH.FOREARM;
        else if(aux[0].equals("g_wrist"))
            return ID.A_GIRTH.WRIST;
        else if(aux[0].equals("g_chest"))
            return ID.A_GIRTH.CHEST;
        else if(aux[0].equals("g_abdominal"))
            return ID.A_GIRTH.ABDOMINAL;
        else if(aux[0].equals("g_gluteal"))
            return ID.A_GIRTH.GLUTEAL;
        else if(aux[0].equals("g_thigh"))
            return ID.A_GIRTH.THIGH;
        else if(aux[0].equals("g_leg"))
            return ID.A_GIRTH.LEG;
        return ID.A_GIRTH.NONE;
    }
    private static ID.A_DIAMETER getAnthropometryDiameterID(String string)
    {
        String[] aux = string.split("=");
        if(aux[0].equals("d_humeral"))
            return ID.A_DIAMETER.HUMERAL;
        else if(aux[0].equals("d_femoral"))
            return ID.A_DIAMETER.FEMORAL;
        else if(aux[0].equals("d_bioestiloideo"))
            return ID.A_DIAMETER.BIOESTILOIDEO;
        return ID.A_DIAMETER.NONE;
    }
    public static Patient open(File file)
    {
        PersonalData pd = PersonalData.getPersonalData();
        ArrayList<AnthropometryData> aList = new ArrayList<AnthropometryData>(5);
        ArrayList<Diagnostic> dList = new ArrayList<Diagnostic>(5);
        Patient patient = new Patient(pd,aList,dList);
        try 
        {
            FileReader fr = new FileReader(file);
            BufferedReader fileOne = new BufferedReader(fr);
            String line;
            String value;
            ID.PD id;
            ID.OTHER other;
            ID.A a;
            ID.A_CREASE ac;
            ID.A_GIRTH ag;
            ID.A_DIAMETER ad;
            while((line = fileOne.readLine()) != null)
            {
                id = getPersonalDataID(line);
                value = getValue(line);
                if(id != ID.PD.NONE)
                    patient.setValue(id,value);
                else
                {
                	other = getOtherID(line);
                	if(other != ID.OTHER.NONE)
                	{
                		/*if(other == ID.OTHER.DATE)
                		{
                			patient.addAnthropometryData(AnthropometryData.getAnthropometry());
                			patient.addDiagnostic(new Diagnostic());
                			patient.setIndexCheckup(patient.getTotalCheckup()-1);
                		}else*/
                		if(other == ID.OTHER.DIAGNOSTIC)
                		{
                			line = fileOne.readLine();
                			while(!line.equals("diagnosticEnd"))
                            {
                				value = value + System.getProperty("line.separator") + line;
                				line = fileOne.readLine();
                				if(line == null)
                					break;
                            }
                		}
                		patient.setValue(other,value);
                	}
	                else 
	                {
	                    a = getAnthropometryID(line);
	                    if(a != ID.A.NONE)
	                        patient.setValue(a,value);
	                    else
	                    {
	                        ac = getAnthropometryCreaseID(line);
	                        if(ac != ID.A_CREASE.NONE)
	                            patient.setValue(ac,value);
	                        else
	                        {
	                            ag = getAnthropometryGirthID(line);
	                            if(ag != ID.A_GIRTH.NONE)
	                                patient.setValue(ag,value);
	                            else
	                            {
	                                ad = getAnthropometryDiameterID(line);
	                                if(ad != ID.A_DIAMETER.NONE)
	                                    patient.setValue(ad,value);
	                            }
	                        } 
	                    }
	                }
                }
            }
            fileOne.close();
        }catch(IOException e){}
        //patient.setDate(new Date());
        return patient;
    }
    public static void save(File file,Patient patient)
    {
        try
        {
            DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, new Locale("es","MX"));
            PrintWriter out = new PrintWriter(new FileWriter(file));
            out.print("name="+patient.getName()+System.getProperty("line.separator"));
            out.print("lastName="+patient.getLastName()+System.getProperty("line.separator"));
            out.print("dateBirth="+df.format(patient.getDateBirth())+System.getProperty("line.separator"));
            out.print("gender="+patient.getGender()+System.getProperty("line.separator"));
            out.print("occupation="+patient.getOccupation()+System.getProperty("line.separator"));
            out.print("coachsName="+patient.getCoachsName()+System.getProperty("line.separator"));
            out.print("sport="+patient.getSport()+System.getProperty("line.separator"));
            out.print("category="+patient.getCategory()+System.getProperty("line.separator"));
            out.print("institution="+patient.getInstitution()+System.getProperty("line.separator"));
            for(int i = 0; i < patient.getTotalCheckup(); i++)
            {
            	patient.setIndexCheckup(i);
	            out.print("date="+df.format(patient.getDate())+System.getProperty("line.separator"));
	            out.print("currentWeight="+patient.getAnthropometryValue(ID.A.CURRRENT_WEIGHT)+System.getProperty("line.separator"));
	            out.print("targetWeight="+patient.getAnthropometryValue(ID.A.TARGET_WEIGHT)+System.getProperty("line.separator"));
	            out.print("size="+patient.getAnthropometryValue(ID.A.SIZE)+System.getProperty("line.separator"));
	            out.print("c_subscapularis="+patient.getAnthropometryValue(ID.A_CREASE.SUBSCAPULARIS)+System.getProperty("line.separator"));
	            out.print("c_tricep="+patient.getAnthropometryValue(ID.A_CREASE.TRICEP)+System.getProperty("line.separator"));
	            out.print("c_bicep="+patient.getAnthropometryValue(ID.A_CREASE.BICEP)+System.getProperty("line.separator"));
	            out.print("c_forearm="+patient.getAnthropometryValue(ID.A_CREASE.FOREARM)+System.getProperty("line.separator"));
	            out.print("c_supraspinal="+patient.getAnthropometryValue(ID.A_CREASE.SUPRASPINAL)+System.getProperty("line.separator"));
	            out.print("c_suprailiac="+patient.getAnthropometryValue(ID.A_CREASE.SUPRAILIAC)+System.getProperty("line.separator"));
	            out.print("c_abdLeft="+patient.getAnthropometryValue(ID.A_CREASE.ABD_LEFT)+System.getProperty("line.separator"));
	            out.print("c_abdRight="+patient.getAnthropometryValue(ID.A_CREASE.ABD_RIGHT)+System.getProperty("line.separator"));
	            out.print("c_thigh="+patient.getAnthropometryValue(ID.A_CREASE.THIGH)+System.getProperty("line.separator"));
	            out.print("c_leg="+patient.getAnthropometryValue(ID.A_CREASE.LEG)+System.getProperty("line.separator"));
	            out.print("c_pectoral="+patient.getAnthropometryValue(ID.A_CREASE.PECTORAL)+System.getProperty("line.separator"));
	            out.print("g_relaxedArm="+patient.getAnthropometryValue(ID.A_GIRTH.RELAXED_ARM)+System.getProperty("line.separator"));
	            out.print("g_contractedArm="+patient.getAnthropometryValue(ID.A_GIRTH.CONTRACTED_ARM)+System.getProperty("line.separator"));
	            out.print("g_forearm="+patient.getAnthropometryValue(ID.A_GIRTH.FOREARM)+System.getProperty("line.separator"));
	            out.print("g_wrist="+patient.getAnthropometryValue(ID.A_GIRTH.WRIST)+System.getProperty("line.separator"));
	            out.print("g_chest="+patient.getAnthropometryValue(ID.A_GIRTH.CHEST)+System.getProperty("line.separator"));
	            out.print("g_abdominal="+patient.getAnthropometryValue(ID.A_GIRTH.ABDOMINAL)+System.getProperty("line.separator"));
	            out.print("g_gluteal="+patient.getAnthropometryValue(ID.A_GIRTH.GLUTEAL)+System.getProperty("line.separator"));
	            out.print("g_thigh="+patient.getAnthropometryValue(ID.A_GIRTH.THIGH)+System.getProperty("line.separator"));
	            out.print("g_leg="+patient.getAnthropometryValue(ID.A_GIRTH.LEG)+System.getProperty("line.separator"));
	            out.print("d_humeral="+patient.getAnthropometryValue(ID.A_DIAMETER.HUMERAL)+System.getProperty("line.separator"));
	            out.print("d_femoral="+patient.getAnthropometryValue(ID.A_DIAMETER.FEMORAL)+System.getProperty("line.separator"));
	            out.print("d_bioestiloideo="+patient.getAnthropometryValue(ID.A_DIAMETER.BIOESTILOIDEO)+System.getProperty("line.separator"));
	            out.print("diagnostic="+patient.getDianostic()+System.getProperty("line.separator")+"diagnosticEnd"+System.getProperty("line.separator"));
            }
            out.close();
        }catch(IOException e){}
    }
}
